package servlet;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.DaoFactory;
import dao.EmpDao;
import entity.Emp;

public class EmpMngtServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String uri = request.getRequestURI();
		String path = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
		if("/list".equals(path)){
			EmpDao dao = DaoFactory.getInstance(EmpDao.class);
			List<Emp> emps = null;
			try {
				emps = dao.findAll();
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServletException(e);
			}
			request.setAttribute("emps", emps);
			request.getRequestDispatcher("empList.jsp").forward(request, response);
		}else if("/add".equals(path)){
			String name = request.getParameter("name");
			double sal = Double.parseDouble(request.getParameter("sal"));
			int age = Integer.parseInt(request.getParameter("age"));
			Emp emp = new Emp(name,sal,age);
			EmpDao dao=DaoFactory.getInstance(EmpDao.class);
			try {
				dao.save(emp);
				response.sendRedirect("list.do");
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServletException(e);
			}
		}else if("/delete".equals(path)){
			int id = Integer.parseInt(request.getParameter("id"));
			EmpDao dao = DaoFactory.getInstance(EmpDao.class);
			try {
				dao.delete(id);
				response.sendRedirect("list.do");
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServletException(e);
			}
		}else if("/update".equals(path)){
			int id=Integer.parseInt(request.getParameter("id"));
			String name=request.getParameter("name");
			double sal = Double.parseDouble(request.getParameter("salary"));
			int age = Integer.parseInt(request.getParameter("age"));
			Emp e = new Emp(name, sal, age);
			e.setId(id);
			EmpDao dao =DaoFactory.getInstance(EmpDao.class);
			try {
				dao.update(e);
				response.sendRedirect("list.do");
			} catch (Exception e1) {
				e1.printStackTrace();
				throw new ServletException(e1);
			}
		}else if("/regist".equals(path)){
			HttpSession session = request.getSession();
			String numberOfSession=(String) session.getAttribute("number");
			String number=request.getParameter("number").toUpperCase();
			String error="验证码错误";
			if(!numberOfSession.equals(number)){
				request.setAttribute("error", error);
				request.getRequestDispatcher("regist.jsp").forward(request, response);
				return;
			}
			String username=request.getParameter("username");
			EmpDao dao = DaoFactory.getInstance(EmpDao.class);
			try {
				Emp e = dao.findByUsername(username);
				String userExisted="用户名己存在，请更换";
				if(e!=null){
					request.setAttribute("userExisted", userExisted);
					request.getRequestDispatcher("regist.jsp").forward(request, response);
					return;
				}
				e=new Emp();
				e.setName(username);
				dao.save(e);
				out.print("registered successfully");
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServletException(e);
			}
		}else if("/login".equals(path)){
			String name=request.getParameter("name");//中文 肯林 得到的结果为  &#32943;&#26519;
			//中文名处理
			if(name.startsWith("&#")){
				name=name.replaceAll("&#", "");
				String[] arr=name.split(";");
				name="";
				for (String s : arr) {
					name+=(char)(Integer.parseInt(s));
				}
			}
			EmpDao dao = DaoFactory.getInstance(EmpDao.class);
			String userNotExsited="user not existed";
			try {
				Emp e = dao.findByUsername(name);
				if(e==null){
					request.setAttribute("userNotExisted", userNotExsited);
					request.getRequestDispatcher("login.jsp").forward(request, response);
					return;
				}
				HttpSession session = request.getSession();
				session.setAttribute("user", e);
				response.sendRedirect("main.jsp");
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServletException(e);
			}
		}else if("/logout".equals(path)){
			HttpSession session=request.getSession();
			session.invalidate();
			response.sendRedirect("login.jsp");
		}else if("/loadEmp".equals(path)){
			int id =Integer.parseInt(request.getParameter("id"));
			EmpDao dao = DaoFactory.getInstance(EmpDao.class);
			try {
				Emp e = dao.findById(id);
				request.setAttribute("e", e);
				request.getRequestDispatcher("updateEmp.jsp").forward(request, response);
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServletException(e);
			}
		}
	}
}
